package com.kmmlearn.findtime.android.ui.animation

import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp


data class ButtonUIProperties(
    val backgroundColor: Color,
    val textColor: Color,
    val roundedCorner: Int,
    val buttonWith: Dp
)

enum class ButtonState(val ui: ButtonUIProperties) {
    IDLE(ButtonUIProperties(Color.White, Color.Gray, 6, 300.dp)),
    PRESSED(ButtonUIProperties(Color.Red, Color.White, 50, 70.dp))
}

@Composable
fun AnimatedButton(modifier: Modifier = Modifier) {
    var buttonState by remember { mutableStateOf(ButtonState.IDLE) }
    Box(modifier) {
        AnimatedContent(
            targetState = buttonState,
            transitionSpec = {
                fadeIn(tween(3000)) with fadeOut(tween(3000))
            }
        ) {
            Button(
                border = BorderStroke(1.dp, it.ui.textColor),
                modifier = Modifier.size(it.ui.buttonWith, 70.dp),
                shape = RoundedCornerShape(it.ui.roundedCorner),
                colors = ButtonDefaults.buttonColors(containerColor = it.ui.backgroundColor),
                onClick = {
                    buttonState = if (buttonState == ButtonState.IDLE) {
                        ButtonState.PRESSED
                    } else {
                        ButtonState.IDLE
                    }
                }
            ) {
                if (it == ButtonState.IDLE) {
                    Row {
                        Icon(
                            imageVector = Icons.Default.FavoriteBorder,
                            contentDescription = null,
                            modifier = Modifier.size(24.dp),
                            tint = it.ui.textColor
                        )
                        Spacer(modifier = Modifier.width(16.dp))
                        Text(
                            text = "ADD TO FAVORITES",
                            color = it.ui.textColor,
                            modifier = Modifier.align(Alignment.CenterVertically)
                        )
                    }

                } else {
                    Icon(
                        imageVector = Icons.Default.Favorite,
                        contentDescription = null,
                        modifier = Modifier.size(24.dp),
                        tint = it.ui.textColor
                    )
                }
            }
        }
    }
}

@Composable
fun IdleButton() {
    Button(
        border = BorderStroke(1.dp, Color.Gray),
        modifier = Modifier.size(300.dp, 70.dp),
        shape = RoundedCornerShape(6),
        colors = ButtonDefaults.buttonColors(containerColor = Color.White),
        onClick = {}) {
        Row {
            Icon(
                imageVector = Icons.Default.FavoriteBorder,
                contentDescription = null,
                modifier = Modifier.size(24.dp),
                tint = Color.Gray
            )
            Spacer(modifier = Modifier.width(16.dp))
            Text(
                text = "ADD TO FAVORITES",
                color = Color.Gray,
                modifier = Modifier.align(Alignment.CenterVertically)
            )
        }
    }
}

@Composable
fun PressedButton() {
    Button(
        border = BorderStroke(1.dp, Color.Red),
        modifier = Modifier.size(70.dp, 70.dp),
        shape = RoundedCornerShape(50),
        colors = ButtonDefaults.buttonColors(containerColor = Color.Red),
        onClick = {}
    ) {
        Icon(
            imageVector = Icons.Default.Favorite,
            contentDescription = null,
            modifier = Modifier.size(24.dp),
            tint = Color.White
        )
    }
}

@Preview
@Composable
fun IdleButtonPreview() {
    IdleButton()
}

@Preview
@Composable
fun PressedButtonPreview() {
    PressedButton()
}

@Preview
@Composable
fun AnimatedButtonPreview() {
    // 创建一个Box，使Box容器填充屏幕，并将AnimatedButton组件添加到Box中，居中显示
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(),
        contentAlignment = Alignment.Center
    ) {
        AnimatedButton()
    }
}
